package com.huobao.yunying.activitys_welcome;

import android.content.Intent;
import android.os.Handler;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.ImageView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baidu.android.pushservice.PushConstants;
import com.baidu.android.pushservice.PushManager;
import com.huobao.yunying.activitys.Act_Main;
import com.huobao.yunying.activitys_log_reg.Act_Login;
import com.huobao.yunying.base.HuobaoBaseActivity;
import com.huobao.yunying.https.Http;
import com.huobao.yunying.https.HttpConst;
import com.huobao.yunying.https.HttpListener;
import com.huobao.yunying.https.HttpUtil;
import com.huobao.yunying.utils.SessionUtils;
import com.huobao.yunying.utils.Utils;
import com.huobao.zhangying.R;
import com.xrz.sxm.base.dialog.BaseDialog;
import com.xrz.sxm.base.utils.ScreenUtil;

import org.xutils.view.annotation.ViewInject;

import java.util.Map;

public class Act_WelCome extends HuobaoBaseActivity implements AnimationListener,BaseDialog.OnDialogSelectListener {

	@ViewInject(R.id.imageview)
	private ImageView imageview;

	private Animation mFadeInScale;

	private int type = 0;

	@Override
	protected int getContentView() {
		return R.layout.layout_welcome;
	}

	@Override
	protected String getTitleText() {
		return null;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		return false;
	}

	@Override
	protected void init() {
//		Sms.start(this);
//		BaseDialog.showPermissionDialog(this, this);
		PushManager.startWork(getApplicationContext(), PushConstants.LOGIN_TYPE_API_KEY,"TBYH7PSHCqRZZm0d7Ignd4TF");
		ScreenUtil.getInstant().init(this);
		if (Utils.checkIsFristLunch(context)) {
			Intent intent = new Intent(this, Act_welcome_page.class);
			startActivity(intent);
			finish();
			return;
		}
		mAbTitleBar.setVisibility(View.GONE);
		checkLogin();
	}
	@Override
	protected boolean isCheckPermission() {
		return false;
	}

	@Override
	public void onAnimationStart(Animation animation) {

	}

	@Override
	public void onAnimationEnd(Animation animation) {
		startActivity(new Intent(this, Act_Main.class));
	}

	@Override
	public void onAnimationRepeat(Animation animation) {

	}
	private Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			switch(msg.what){
				case 1:
					startActivity(new Intent(context, Act_Login.class));
					break;
				case 2:
					startActivity(new Intent(context, Act_Main.class));
					break;
				case 3:
					startActivity(new Intent(context, Act_Main.class).putExtra("role",2));
					break;
			}
			finish();
		};
	};

	private void checkLogin() {
		// if (SessionUtils.isTenday(context)) {
		// if (SessionUtils.checkTendayLogin(context)) {
		// // 如果点击了记住10天，并且在10天之内
		if (TextUtils.isEmpty(SessionUtils.getUserName(context)) == false
				&& TextUtils.isEmpty(SessionUtils.getPwd(context)) == false)
			login();
		else {
			new Thread(){
				public void run() {
					try {
						sleep(1000);
					} catch (InterruptedException e) {
					}
					handler.sendEmptyMessage(1);
				};
			}.start();
		}
		// } else {
		// startActivity(new Intent(this, Act_Main.class));
		// finish();
		// }
		// } else {
		// startActivity(new Intent(this, Act_Main.class));
		// finish();
		// }
	}

	private long tempTime;
	private void login() {
		Map<String, String> map = HttpUtil.getParams(HttpConst.FRIST_KEY, "login", "user_name",
				SessionUtils.getUserName(context), "password", SessionUtils.getPwd(context));
		tempTime = System.currentTimeMillis();
		Http.getInstant().sendRequest(context, false, map, new HttpListener() {

			@Override
			public void onHttpSuccess(String result) {
				JSONObject jsonObject = JSON.parseObject(result);
				jsonObject = jsonObject.getJSONObject("retval");
				int role = jsonObject.getIntValue("user_role");
				if (role == 2) {
					if((System.currentTimeMillis() - tempTime) > 2000){
						handler.sendEmptyMessage(3);
						return;
					}
					new Thread(){
						public void run() {
							try {
								sleep(1000);
							} catch (InterruptedException e) {
							}
							handler.sendEmptyMessage(3);
						};
					}.start();
				} else {
					if((System.currentTimeMillis() - tempTime) > 2000){
						handler.sendEmptyMessage(2);
						return;
					}
					new Thread(){
						public void run() {
							try {
								sleep(1000);
							} catch (InterruptedException e) {
							}
							handler.sendEmptyMessage(2);
						};
					}.start();

				}

			}

			@Override
			public void onHttpError(String error) {
				Toast.makeText(context, "登录失败", Toast.LENGTH_SHORT).show();
				startActivity(new Intent(context, Act_Login.class));
				finish();
			}
		});
	}

	@Override
	public void onSelect(String input) {

	}
}